package com.kyub.spring.gears.services.impl;

import java.beans.PropertyEditorSupport;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Iterator;
import java.util.Map;

import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.multipart.support.ByteArrayMultipartFileEditor;

import com.kyub.spring.gears.introspect.PojoDescriptorFactory;
import com.kyub.spring.gears.services.PropertyEditorRegister;

public class DefaultPropertyEditorRegistar implements PropertyEditorRegister {
	
	private String dateFormat = "dd-MM-yyyy";

	/* (non-Javadoc)
	 * @see com.kyub.spring.gears.web.PropertyEditorRegister#registerPropertyEditors(org.springframework.web.bind.WebDataBinder, java.util.Map)
	 */
	public void registerPropertyEditors(WebDataBinder binder, Map<Class, PropertyEditorSupport> map) {
		Iterator i = map.entrySet().iterator();
        while (i.hasNext()) {
            final Map.Entry o = (Map.Entry) i.next();
            final PropertyEditorSupport entry = (PropertyEditorSupport) o.getValue();
            binder.registerCustomEditor((Class) o.getKey(), entry);
        }
        
        SimpleDateFormat dateFormat = new SimpleDateFormat(this.dateFormat);
        dateFormat.setLenient(false);
        binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, false));
        binder.registerCustomEditor(byte[].class, new ByteArrayMultipartFileEditor());
        
	}

	public String getDateFormat() {
		return dateFormat;
	}

	public void setDateFormat(String dateFormat) {
		this.dateFormat = dateFormat;
	}
	
	
}